<?php

require('pagina.php');
require('database.php');
require('auth.php');
require('core.php');

// Verifica se esta autenticado com pelo menos nivel 3
validaPermissao(3);

// Verifica se ha locacao para ver
if (!$_GET['id'])
{
	forbidden();
}

// Busca todos os dados da locacao
$location = query_fetch("SELECT * FROM locations WHERE id='{$_GET['id']}' LIMIT 1");

// Se nao existe, cai fora
if (!$location)
{
    response_404();
}

//Trata operate/do not operate locacao
if (isset($_GET['fire'] ))
{
	query_bd("UPDATE locations SET operating=FALSE where id={$location['id']}");
	$location['operating'] = False;
	$msg = 'This location is not longer operated.';
}
else if (isset($_GET['hire'] ))
{
	query_bd("UPDATE locations SET operating=TRUE where id={$location['id']}");
	$location['operating'] = True;
	$msg = 'This location is now being operated.';
}

// Busca contatos
$location_contact = query_fetch("SELECT * FROM contacts WHERE id={$location['locationcontact']} LIMIT 1");
$manager_contact = query_fetch("SELECT * FROM contacts WHERE id={$location['managercontact']} LIMIT 1");

// Inicia cabecalho da pagina
pagina_inicio('ParkSys - Location');
pagina_head();

// Mostra opcoes
echo "<ul id=opcoes>";
echo "<li><a href='location.php?id={$location['id']}&amp;print=1' target='_blank'><img src='img/print.png' alt=''>Print</a></li>";
echo "<li><a href='location_edit.php?id={$location['id']}'><img src='img/edit.png' alt=''>Edit Location</a></li>";

if ($location['operating'])
{
	echo "<li class='negative'><a href='location.php?id={$location['id']}&amp;fire=1' ",
	"onClick='return confirm(\"Do you really want to set this location unoperating?\");'>",
	"<img src='img/cancel.png' alt=''>Set unoperating</a></li>";
}
else
{
	echo "<li class='positive'><a href='location.php?id={$location['id']}&amp;hire=1'>",
	"<img src='img/ok.png' alt=''>Set location operating</a></li>";
}

echo "<li><a href='reports.php?location={$location['id']}' ><img src='img/report.png' alt=''>Reports</a></li>";
echo "<li><a href='incomes_expenses.php?location={$location['id']}'><img src='img/payday.png' alt=''>Inc. &amp; Exp.</a></li>";
echo "<li><a href='statistics.php?location={$location['id']}'><img src='img/report.png' alt=''>Statistics</a></li>";

echo "<li><a href='incomes_expenses.php?inc_category=none&amp;exp_category=", ACCIDENT, "&amp;location={$location['id']}'>",
	"<img src='img/exclamation.png' alt=''>Accidents</a></li>";
echo "<li class='negative'><a href='expense_new.php?category=", ACCIDENT, "&amp;references_to=", LOCATION, "&amp;location={$location['id']}'>",
	"<img src='img/exclamation.png' alt=''>New Accident</a></li>";

echo "</ul><br>";

echo "<div class=dados_estaticos>";
echo "<h1>Location</h1><br>";
echo "<dl>";
echo "<dt>Location name</dt><dd>{$location_contact['name']}",
	($location['operating']? "" : "<span class=negativo>(not operating)</span>"), "</dd>";
echo "<dt>Working time</dt><dd>{$location['workingtime']}</dd>";

/* busca por tickets ranges e mostra */
$ticket_range_result = query_bd("SELECT ticket_start, ticket_end FROM ticket_ranges WHERE location_id={$location['id']}");
while ($ticket_range = mysql_fetch_array($ticket_range_result))
{
    echo "<dt>Tickets</dt><dd>",  sprintf("%06d", $ticket_range['ticket_start']), " to ",
        sprintf("%06d", $ticket_range['ticket_end']), "(", ($ticket_range['ticket_end']-$ticket_range['ticket_start']), ")</dd>";
}
unset($ticket_range_result);
echo "</dl>";

echo "<hr><h2>Location Contact</h2><br>";
echo "<dl>";
echo "<dt>Address</dt><dd><address>",
"{$location_contact['number']}, {$location_contact['street']}, {$location_contact['complement']} ",
"zip: {$location_contact['zipcode']}, {$location_contact['city']} - {$location_contact['state']}.</address></dd>";
if ($location_contact['phone'])
	echo "<dt>Phone number</dt><dd>{$location_contact['phone']}</dd>";
if ($location_contact['cellphone'])
	echo "<dt>Mobile number</dt><dd>{$location_contact['cellphone']}</dd>";
if ($location_contact['email'])
	echo "<dt>Email</dt><dd>{$location_contact['email']}</dd>";
echo "</dl>";
    
echo "<hr><h2>Manager Contact</h2><br>";
echo "<dl>";
echo "<dt>Manager name</dt><dd>{$manager_contact['name']}</dd>";
echo "<dt>Address</dt><dd><address>",
"{$manager_contact['number']}, {$manager_contact['street']}, {$manager_contact['complement']} ",
"zip: {$manager_contact['zipcode']}, {$manager_contact['city']} - {$manager_contact['state']}.</address></dd>";
if ($manager_contact['phone'])
	echo "<dt>Phone number</dt><dd>{$manager_contact['phone']}</dd>";
if ($manager_contact['cellphone'])
	echo "<dt>Mobile number</dt><dd>{$manager_contact['cellphone']}</dd>";
if ($manager_contact['email'])
	echo "<dt>Email</dt><dd>{$manager_contact['email']}</dd>";
echo "</dl>";
    
echo "<hr><h2>Location Information</h2><br>";
echo "<dl>";
echo "<dt>Manager payrate</dt><dd>&#36;{$location['manager_payrate']}</dd> ";
echo "<dt>Valet payrate</dt><dd>&#36;{$location['valet_payrate']}</dd>";
echo "<dt>Insurance tax per car</dt><dd>&#36;{$location['insurance_tax']}</dd>";
// echo "<dt>Tip percent income</dt><dd>", ($location['tip_percent'] > 0? $location['tip_percent']*100.0 . "%": "none"), "</dd>";

if ($location['obs'])
	echo "<dt>Observation</dt><dd>{$location['obs']}</dd>";
echo "</dl>";
    
echo "</div>";

// Finaliza pagina
pagina_fim();
?>
